package edu.mtu.citizenscience.cs.sdk.json;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Response<E> {
	private int generated;
	private String payload;
	private Request<?> request;
	private Error error;
	private Class<?> playloadType;
	
	public boolean havePayload()
	{
		return ! (this.payload.equals(null) || this.payload.equals("null"));
	}
	
	@SuppressWarnings("unchecked")
	public E getPayload() throws JsonParseException, JsonMappingException
	{
		
		try
		{
			return (E) new ObjectMapper().readValue(this.payload, this.playloadType);
		}
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		
		return null;
	}
	
	public void setPayload(E payload) throws JsonProcessingException
	{
		this.payload = new ObjectMapper().writeValueAsString(payload);
	}
	
	public Class<?> getPlayloadType() 
	{
		return playloadType;
	}

	public void setPlayloadType(Class<?> playloadType) 
	{
		this.playloadType = playloadType;
	}

	public int getGenerated() 
	{
		return generated;
	}

	public void setGenerated(int generated) 
	{
		this.generated = generated;
	}

	public void setPayload(String payload) 
	{
		this.payload = payload;
	}

	public Request<?> getRequest() 
	{
		return request;
	}
	
	public void setRequest(Request<?> request) 
	{
		this.request = request;
	}
	
	public Error getError() 
	{
		return error;
	}
	
	public void setError(Error error) 
	{
		this.error = error;
	}
}
